Option Explicit
'зǼҲ
Dim myCbar As CbarEventCls
Sub O_Sample006()
    Dim myCnt As CommandBarControl
    MenuDelete
    Set myCbar = New CbarEventCls
    Set myCnt = Application.VBE.CommandBars("Code Window") _
        .Controls.Add(msoControlButton)
    myCnt.Caption = "{X"
    Set myCbar.myEvents = Application.VBE.Events _
        .CommandBarEvents(myCnt)
End Sub

Sub MenuDelete()
    'nsWYwsbɴNNR
    On Error Resume Next
    Do
        Err.Clear
        Application.VBE _
            .CommandBars("Code Window") _
            .Controls("{X") _
            .Delete
    Loop While Err.Number = 0
    On Error GoTo 0
End Sub

Sub CodeWindowClose()
    '{X
    Dim myWd As VBIDE.Window
    For Each myWd In Application.VBE.Windows
        Select Case myWd.Type
            Case vbext_wt_CodeWindow, vbext_wt_Designer
                myWd.Close
        End Select
    Next
End Sub

'OҲCbarEventCls
Public WithEvents myEvents As CommandBarEvents
Private Sub myEvents_Click( _
                           ByVal CommandBarControl As Object, _
                           handled As Boolean, _
                           CancelDefault As Boolean)
    CodeWindowClose				'n檺{ǦW
End Sub
